package com.example.practice.controller;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URLEncoder;

/**
 * TODO
 *
 * @author zgq
 * @date 2020/10/20 15:58
 */
@Slf4j
@RestController
@RequestMapping("wx")
public class WeXinController {

    @Autowired
    private WxMpService wxMpService;

    @GetMapping("/auth")
    public String authorize(@RequestParam("returnUrl") String returnUrl) {
        String url = "http://mrz5ks.natappfree.cc/wx/userInfo";
        String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl));
        System.out.println("微信网页授权：获取凑得，redirectURL=" + redirectUrl);
        return "redirect" + redirectUrl;
    }

    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,
                           @RequestParam("state") String returnUrl) throws Exception {
        log.info("【微信网页授权】code={}", code);
        log.info("【微信网页授权】state={}", returnUrl);
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        } catch (WxErrorException e) {
            log.info("【微信网页授权】{}", e);
            throw new Exception(e.getError().getErrorMsg());
        }
        String openId = wxMpOAuth2AccessToken.getOpenId();
        log.info("【微信网页授权】openId={}", openId);
        return "redirect:" + returnUrl + "?openid=" + openId;
    }

    @GetMapping("/createMenu")
//    public String menuCreateSample(@PathVariable String appid) throws WxErrorException, MalformedURLException {
    public String menuCreateSample() throws WxErrorException, MalformedURLException {
        String appid = "wx734eaf9a775eaf29";
        WxMenu menu = new WxMenu();
        WxMenuButton button1 = new WxMenuButton();
        button1.setName("简省");
        button1.setType(WxConsts.MenuButtonType.VIEW);
        button1.setUrl("https://www.baidu.com");
        menu.getButtons().add(button1);


        WxMenuButton button2 = new WxMenuButton();
        button2.setName("服务商");
        button2.setType(WxConsts.MenuButtonType.VIEW);
        button2.setUrl("https://www.360.com/");
        menu.getButtons().add(button2);

//        WxMenuButton button3 = new WxMenuButton();
//        button3.setName("下载中心");
//
//        WxMenuButton button31 = new WxMenuButton();
//        button31.setName("简省");
//        button31.setType(WxConsts.MenuButtonType.VIEW);
//        button31.setUrl("https://jsh5.znyktec.com/pages/users/download/index");
//
//        WxMenuButton button32 = new WxMenuButton();
//        button32.setName("服务商");
//        button32.setType(WxConsts.MenuButtonType.VIEW);
//        button32.setUrl("https://jsh5.znyktec.com/pages/users/download/index2");
//
//        button3.getSubButtons().add(button31);
//        button3.getSubButtons().add(button32);

        WxMenuButton button3 = new WxMenuButton();
        button3.setName("下载中心1232312");
        button3.setType(WxConsts.MenuButtonType.VIEW);
        button3.setUrl("https://www.hao123.com");

        menu.getButtons().add(button3);

        this.wxMpService.switchover(appid);
        return this.wxMpService.getMenuService().menuCreate(menu);
    }
}
